package com.jt.ktdemo.http

import com.jt.ktdemo.bean.*
import com.jt.ktdemo.utils.GsonUtils
import com.jt.ktdemo.utils.LogUtil
import com.jt.ktdemo.utils.MD5Util
import com.jt.ktdemo.utils.SpUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import java.io.File

object HTTPAPI {

    private const val baseUrl: String =
        "http://ec2-13-59-112-96.us-east-2.compute.amazonaws.com:8080/"

    //注册
    private const val POST_REGISTER_URL = "irealcare/user/registerByEmail"

    //登录
    private const val POST_LOGIN_URL = "irealcare/user/loginByEmail"

    //消息首页
    private const val POST_MESSAGE_OBTION = "message/messageMain"

    //报告消息
    private const val POST_MESSAGE_REPORT = "message/messageReport"

    //系统消息
    private const val POST_MESSAGE_SYSTEM = "message/messageSystem"

    //个人信息
    private const val POST_PERSION = "irealcare/user/PersonInfo"

    //发送验证码
    private const val POST_SEND_CODE = "irealcare/user/sendMailCode"

    //上传头像
    private const val UPDATE_FACE = "irealcare/user/UpdateFace"

    //更新密码
    private const val POST_UPDATE_PWD = "irealcare/user/updatePasswordNEW"

    //验证 code
    private const val POST_CHECK_CODE = "irealcare/user/verifyMailCode"

    //更新个人信息
    private const val POST_UPDATE_USERINFO = "irealcare/user/userPersonalInformation"

    /**
     * 登录
     */
    fun toLogin(email: String, pwd: String, callback: StringCallback) {
        LogUtil.e("注册地址-->$baseUrl$POST_LOGIN_URL")
        var login = LoginRequestBean()
        login.email = email
        login.password = MD5Util.encode(pwd)

        var inputStr = GsonUtils.toJson(login)
        LogUtil.e("注册参数-->$inputStr")

        OkGo.post("$baseUrl$POST_LOGIN_URL")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", inputStr)
            .execute(callback)
    }

    /**
     * 注册
     */
    fun toRegister(
        name: String,
        pwd: String,
        email: String,
        phoneNumber: String,
        callback: StringCallback
    ) {

        LogUtil.e("注册地址-->${Constants.POST_REGISTER_URL}")
        var kt = KtRegisterRequestBean()
        kt.username = name
        kt.email = email
        kt.password = MD5Util.encode(pwd)
        kt.phoneNumber = phoneNumber

        var inputStr = GsonUtils.toJson(kt).toString()
        LogUtil.e("注册入参-->$inputStr")

        OkGo.post("$baseUrl$POST_REGISTER_URL")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params(
                "req", GsonUtils.toJson(kt)
            ).execute(callback)
    }

    /**
     * 消息首页数据
     */
    fun getMessageObtion(callback: StringCallback) {
        var bean: MessageHomeBean = MessageHomeBean()
        bean.setSession(SpUtils["module_session", ""] as String)
        bean.setPage(1)
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_MESSAGE_OBTION")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            /**
             * 传递session
             * 参数1
             *      SpUtils["module_session", ""] as String --> 6ad1f941-2c4d-429f-a2f9-579691859a86
             * 参数2
             *      GsonUtils.toJson(bean) --> {"session":"6ad1f941-2c4d-429f-a2f9-579691859a86"}
             */
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 报告消息
     */
    fun getReportMessageList(page: Int, callback: StringCallback) {
        var bean: MessageHomeBean = MessageHomeBean()
        bean.setPage(page)
        bean.setSession(SpUtils["module_session", ""] as String)
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_MESSAGE_REPORT")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            /**
             * 传递session
             * 参数1
             *      SpUtils["module_session", ""] as String --> 6ad1f941-2c4d-429f-a2f9-579691859a86
             * 参数2
             *      GsonUtils.toJson(bean) --> {"session":"6ad1f941-2c4d-429f-a2f9-579691859a86"}
             */
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 获取系统消息
     */
    fun getSystemMessageList(page: Int, callback: StringCallback) {
        var bean: MessageHomeBean = MessageHomeBean()
        bean.setPage(page)
        bean.setSession(SpUtils["module_session", ""] as String)

        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_MESSAGE_SYSTEM")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     *  个人信息
     */
    fun getPersion(callback: StringCallback) {
        var bean: InfoBean = InfoBean()
        bean.setSession(SpUtils["module_session", ""] as String)
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_PERSION")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 获取邮箱验证码
     */
    fun sendCode(name: String, email: String, callback: StringCallback) {
        var bean: SendEmailCodeRequestBean = SendEmailCodeRequestBean(
            SpUtils["module_session", ""] as String,
            name, email
        )
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_SEND_CODE")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 验证  验证码
     */
    fun checkCode(mailCode: String, callback: StringCallback) {
        var bean: CheckCodeRequestBean =
            CheckCodeRequestBean(mailCode)
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_CHECK_CODE")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 更新密码
     */
    fun updatePwd(pwd: String, newPassword: String, callback: StringCallback) {
        var bean: UpdatePwdRequestBean = UpdatePwdRequestBean(
            SpUtils["module_session", ""] as String,
            MD5Util.encode(pwd), MD5Util.encode(newPassword)
        )
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_UPDATE_PWD")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }


    /**
     * 更新个人信息
     */
    fun updatePersonnelInfomation(bean: UpdatePersonelInfomationBean, callback: StringCallback) {
        bean.setSession(SpUtils["module_session", ""] as String)
        bean.setUid(Integer.valueOf(SpUtils["module_uid", ""] as String))
        LogUtil.e("提交参数-->" + GsonUtils.toJson(bean))
        OkGo.post("$baseUrl$POST_UPDATE_USERINFO")
            .tag(this)
            .connTimeOut(30000)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("req", GsonUtils.toJson(bean))
            .execute(callback)
    }

    /**
     * 上传图片
     */
    fun updateHeadPhoto(file: File, callback: StringCallback) {
        OkGo.post("$baseUrl$UPDATE_FACE")
            .tag(this)
            .isMultipart(true)
            .headers("clienttype", "1")
            .headers("deviceid", "1")
            .headers("version", "1.0.0")
            .params("files", file)
            .execute(callback)
    }
}
